package com.kaesun.ufit.weight;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kaesun.ufit.hardware.Buzzer;
import com.kaesun.ufit.tools.ConfigData;
import com.kaesun.ufit.tools.ToolbarView;

public class SetupActivity extends Activity implements View.OnClickListener, IOnHandlerMessage {

	private static final int BUTTON_IDS[] = {
		R.id.button_back, R.id.button_home,

		R.id.text_setup_machine, R.id.button_setup_machine,
		R.id.text_setup_momentum, R.id.button_setup_momentum,
		R.id.text_setup_device, R.id.button_setup_device,
		R.id.text_setup_service, R.id.button_setup_service,
		R.id.text_setup_calibration, R.id.button_setup_calibration,
		R.id.text_setup_password, R.id.button_setup_password,
	};

	private static final int REQUEST_MACHINE = 1;
	private static final int REQUEST_MOMENTUM = 2;
	private static final int REQUEST_DEVICCE = 3;
	private static final int REQUEST_SERVICE = 4;
	private static final int REQUEST_CALIBRATION = 5;

	private MainApplication mApp = null;
	private WeakRefHandler mHandler = null;
	private ToolbarView mToolbarView = null;

	private TextView mVersionView = null;
	private AlertDialog mAlertDialog = null;

	private int mPasswordStep = 0;
	private String mPasswordNew = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setup);

		for (int i=0; i<BUTTON_IDS.length; i++) {
			findViewById(BUTTON_IDS[i]).setOnClickListener(this);
		}

		mApp = (MainApplication)getApplication();
		mHandler = new WeakRefHandler((IOnHandlerMessage)this);
		mToolbarView = (ToolbarView)findViewById(R.id.view_toolbar);

		mVersionView = (TextView)findViewById(R.id.text_setup_version);

		mToolbarView.chage2Button();
		mToolbarView.setTitle("Setup");

		mVersionView.setText(MainApplication.PROGRAM_VERSION);
	}

	@Override
	protected void onDestroy() {

		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (null != mAlertDialog) {
			if (mAlertDialog.isShowing()) {
				mAlertDialog.dismiss();
			}
			mAlertDialog = null;
		}

		super.onPause();
	}

	@Override
	protected void onResume() {
		mApp.setHandler(mHandler, true);

		super.onResume();
	}

	public void onClick(View arg0) {
		mApp.timeoutReset();

		Buzzer.delay(50);
		switch (arg0.getId()) {
		case R.id.button_back:
		case R.id.button_home:
			setResult(RESULT_OK);
			finish();
			break;

		case R.id.text_setup_machine:
		case R.id.button_setup_machine:
			showActivity(REQUEST_MACHINE);
			break;
		case R.id.text_setup_momentum:
		case R.id.button_setup_momentum:
			showActivity(REQUEST_MOMENTUM);
			break;
		case R.id.text_setup_device:
		case R.id.button_setup_device:
			showActivity(REQUEST_DEVICCE);
			break;
		case R.id.text_setup_service:
		case R.id.button_setup_service:
			showActivity(REQUEST_SERVICE);
			break;
		case R.id.text_setup_calibration:
		case R.id.button_setup_calibration:
			showActivity(REQUEST_CALIBRATION);
			break;
		case R.id.text_setup_password:
		case R.id.button_setup_password:
			showPasswordDialog(mPasswordStep);
			break;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mApp.timeoutReset();

		return super.onTouchEvent(event);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case REQUEST_MACHINE:
			if (RESULT_OK != resultCode) {
				setResult(RESULT_OK);
				finish(); 
			}
			break;
		case REQUEST_MOMENTUM:
			if (RESULT_OK != resultCode) {
				setResult(RESULT_OK);
				finish(); 
			}
			break;
		case REQUEST_DEVICCE:
			if (RESULT_OK != resultCode) {
				setResult(RESULT_OK);
				finish();
			}
			break;
		case REQUEST_SERVICE:
			if (RESULT_OK != resultCode) {
				setResult(RESULT_OK);
				finish();
			}
			break;
		case REQUEST_CALIBRATION:
			if (RESULT_OK != resultCode) {
				setResult(RESULT_OK);
				finish();
			}
			break;
		}
	}

	@Override
	public void handleMessage(Message msg) {
		switch (msg.what) {
		case MainApplication.HANDLER_TIMEOUT_SECOND:
			mToolbarView.setTime(msg.arg1, msg.arg2);
			break;
		case MainApplication.HANDLER_TIMEOUT_ACTIVITY:
			setResult(RESULT_OK);
			finish();
			break;
		}
	}


	private void showActivity(int req) {
		Intent intent = null;

		switch (req) {
		case REQUEST_MACHINE:
			intent = new Intent(SetupActivity.this, MachineActivity.class);
			break;
		case REQUEST_MOMENTUM:
			intent = new Intent(SetupActivity.this, MomentumActivity.class);
			break;
		case REQUEST_DEVICCE:
			intent = new Intent(SetupActivity.this, DeviceActivity.class);
			break;
		case REQUEST_SERVICE:
			intent = new Intent(SetupActivity.this, ServiceActivity.class);
			break;
		case REQUEST_CALIBRATION:
			intent = new Intent(SetupActivity.this, CalibrationActivity.class);
			break;
		}

		if (null != intent) {
			startActivityForResult(intent, req);
		}
	}

	private void showPasswordDialog(int step) {
		LinearLayout linear = (LinearLayout)View.inflate(this, R.layout.dialog_input, null);
		TextView textView = (TextView)linear.findViewById(R.id.text_title);
		final int fStep = step;
		final EditText fEditInput = (EditText)linear.findViewById(R.id.edit_input);

		switch (fStep) {
		case 0:
			textView.setText(" Current Password ");
			break;
		case 1:
			textView.setText(" New Password ");
			break;
		case 2:
			textView.setText(" Confirm Password ");
			break;
		}
		fEditInput.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);

		if (null != mAlertDialog) {
			if (mAlertDialog.isShowing()) {
				mAlertDialog.dismiss();
			}
			mAlertDialog = null;
		}

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setView(linear)
		.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String input = fEditInput.getText().toString();

				switch (fStep) {
				case 0:
					do {
						String pwd = ConfigData.getPrefString(SetupActivity.this, ConfigData.PREF_SETUP_PASSWORD, ConfigData.DEF_SETUP_PASSWORD);
						if (0 == pwd.compareTo(input)) {
							showPasswordDialog(++mPasswordStep);
						}
					} while (false);
					break;
				case 1:
					mPasswordNew = input;
					showPasswordDialog(++mPasswordStep);
					break;
				case 2:
					if (0 == input.compareTo(mPasswordNew)) {
						ConfigData.setPref(SetupActivity.this, ConfigData.PREF_SETUP_PASSWORD, input);
						mPasswordStep = 0;
					}
					break;
				}
			}
		})
		.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});

		mAlertDialog = builder.create();
		mAlertDialog.show();
	}
}
